<?php
class treePagesEdit extends module {
	public $permissions = array('*'=>array(ROLE_ADMIN, ROLE_SUPERADMIN));
	public $title = "Tree editable pages (administration)";
	public $frame = "admin";
	
	public function run() {
		$rootId = $this->engine->getUrlParam(0)?intval($this->engine->getUrlParam(0)):0;

		$this->xml->root = ($rootId==0)?_("Root"):$this->sql->getValueFromDB("SELECT name FROM tree_pages WHERE id='$rootId' LIMIT 1");
		$this->xml->root['id'] = $rootId;

		$this->tree($rootId);
	}
	
	private function tree($parentId) {
		if (!isset($this->level)) $this->level = 0;
		if (!isset($this->indent)) $this->indent = array();
		if (!isset($this->rowsCount)) $this->rowsCount = -1;
		
		$data = $this->sql->getArrayFromDB("SELECT id, name, date, published FROM tree_pages WHERE parent_id='$parentId' ORDER BY order_key DESC");
		for ($i=0; $i<count($data); $i++) {
			$this->rowsCount++;

			$this->indent[$this->level] = 1;
			$twigEnd = false;
			if ($i+1 < count($data)) $this->indent[$this->level] = 3;
			if ($i+1 == count($data)) {
				$this->indent[$this->level] = 2;
				$twigEnd = true;
			}
			
			$this->xml->row[$this->rowsCount] = "";
			$this->xml->row[$this->rowsCount]['name'] = $data[$i]['name'];
			$this->xml->row[$this->rowsCount]['id'] = $data[$i]['id'];
			$this->xml->row[$this->rowsCount]['date'] = $data[$i]['date'];
			$this->xml->row[$this->rowsCount]['published'] = $data[$i]['published'];

			for ($j=0; $j<count($this->indent); $j++) {
				$this->xml->row[$this->rowsCount]->bullet[$j] = "";
				$this->xml->row[$this->rowsCount]->bullet[$j]['type'] = $this->indent[$j];
			}
			
			if (!$twigEnd) $this->indent[$this->level] = 1;
			else $this->indent[$this->level] = 0;

			$this->level++;
			$this->tree($data[$i]['id']);
		}
		unset($this->indent[$this->level]);
		$this->level--;
	}

	private function createForm($title) {
		$form = new formConstructor($this, "treePage", "", $title);
		$form->addField("parent_id", "", new formHidden(null));
		$form->addField("name", _("Page name"), new formString("/^.{0,255}$/"));
		$form->addField("content", _("Content"), new formVisualEditor(80, 20));
		$form->addField(null, "Meta-Information", new formSeparator());
		$form->addField("title", _("Page title"), new formString("/^.{0,255}$/"));
		$form->addField("keywords", _("Meta-keywords"), new formString("/^.{0,255}$/"));
		$form->addField("description", _("Meta-description"), new formString("/^.{0,255}$/"));
		$form->addField("order_key", _("Order key"), new formString("/^[0-9\.]{0,30}$/"));
		$form->addField('published', _("Published"), new formRadio(array('yes'=>_("Yes"), 'no'=>_("No"))));
		$form->addField(null, _("Save"), new formSubmit());
		return $form;
	}

	private function save($form, $new=false) {
		if (count($form->errors) == 0) {
			try {
				$postData = $form->getAllValues();
				$postData['date'] = new sqlFunction("NOW()");
				if ($new) {
					$this->sql->insertIntoTable("tree_pages", $postData);
				} else {
					$this->sql->updateTable("tree_pages", $postData, array('id' => intval($this->engine->getUrlParam(0))));
				}
				$this->engine->jump("/admin/treePagesEdit/");
			} catch (Exception $e) {
				new pageAlert($this, $e->getMessage());
			}
		} else {
			new pageAlert($this, _("Wrong form filling"));
		}
	}

	public function add() {
		$form = $this->createForm(_("Create tree page"));
		if ($form->submit()) {
			$this->save($form, true);
		} else {
			$form->setValue("parent_id", intval($this->engine->getUrlParam(0)));
			$form->setValue("published", "yes");
		}
		$form->write();
	}

	public function edit() {
		$form = $this->createForm(_("Edit tree page"));
		if ($form->submit()) {
			$this->save($form);
		} else {
			$form->setValues($this->sql->getRowFromDB("SELECT parent_id, name, content, title, keywords, description, order_key, published FROM tree_pages WHERE id=".intval($this->engine->getUrlParam(0))));
		}
		$form->write();
	}

	public function delete() {
		$id = sql::e(intval($this->engine->getUrlParam(0)));
		$this->sql->q("DELETE FROM tree_pages WHERE id='$id' OR parent_id='$id'");
		$this->engine->jump("/admin/treePagesEdit/");
	}

}
?>